package com.syzg.base.person.service.impl;

import com.syzg.base.merge.model.Person;
import com.syzg.base.merge.model.PersonRole;
import com.syzg.base.merge.model.PersonRoleId;
import com.syzg.base.person.dao.PersonDAO;
import com.syzg.base.person.service.PersonService;
import com.syzg.common.config.SysProperties;
import com.syzg.common.exception.BaseException;
import com.syzg.common.service.impl.BaseServiceImpl;
import com.syzg.core.exception.ServiceException;
import com.syzg.core.util.SecurityUtil;
import com.syzg.core.util.StringUtil;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service("personService")
public class PersonServiceImpl extends BaseServiceImpl<Person>
  implements PersonService
{
  private PersonDAO personDAO;

  @Resource
  public void setPersonDAO(PersonDAO personDAO)
  {
    super.setDAO(personDAO);
    this.personDAO = personDAO;
  }

  public void createPerson(Person person, String[] roleIds) throws ServiceException {
    if (isExist(null, new String[] { "account" }, new String[] { person.getAccount() }, person.getClass()))
      throw new BaseException("登录帐号已存在，请使用其他账号！", false);
    if (StringUtil.isEmpty(person.getPassword()))
      person.setPassword(SecurityUtil.md5Hex(SysProperties.DEFAULT_PASSWORD));
    else {
      person.setPassword(SecurityUtil.md5Hex(person.getPassword()));
    }
    this.personDAO.create(person);
    for (int i = 0; i < roleIds.length; i++) {
      String roleId = roleIds[i];
      PersonRole personRole = new PersonRole();
      personRole.setId(new PersonRoleId(person.getId(), roleId));
      this.personDAO.create(personRole);
    }
  }

  public void updatePerson(Person person, String[] roleIds) throws ServiceException {
    updatePerson(person);
    this.personDAO.deletePersonRoleRelation(person.getId());
    int len = roleIds.length;
    for (int i = 0; i < len; i++) {
      String roleId = roleIds[i];
      PersonRole personRole = new PersonRole();
      personRole.setId(new PersonRoleId(person.getId(), roleId));
      this.personDAO.create(personRole);
    }
  }

  public Person findPerson(String account, String password) throws ServiceException
  {
    return this.personDAO.findPerson(account, password);
  }

  public void deletePerson(String[] ids) throws ServiceException {
    if ((ids != null) && (ids.length > 0))
      for (int i = 0; i < ids.length; i++) {
        this.personDAO.deletePersonRoleRelation(ids[i]);
        this.personDAO.deletePerson(ids[i]);
      }
  }

  public void updatePerson(Person person)
    throws ServiceException
  {
    if (isExist(person.getId(), new String[] { "account" }, new String[] { person.getAccount() }, person.getClass()))
      throw new BaseException("用户帐号已存在，请使用其他账号！", false);
    if (person.getPassword().length() != 32)
      person.setPassword(SecurityUtil.md5Hex(person.getPassword()));
    this.personDAO.update(person);
  }

  public Person findPersonById(String id) throws ServiceException {
    return (Person)this.personDAO.findObjectById(id);
  }

  public void updatePassword(String id, String oldPwd, String newPwd) throws ServiceException
  {
    Person person = (Person)this.personDAO.findObjectById(id);
    if ((oldPwd != null) && (!SecurityUtil.md5Hex(oldPwd).equals(person.getPassword()))) {
      throw new BaseException("旧密码错误！", false);
    }
    this.personDAO.executeHql("update Person set password='" + SecurityUtil.md5Hex(newPwd) + "' where id='" + id+"'");
  }
  
  @Override
	public Person findPerson(Person p) throws ServiceException {
		// TODO Auto-generated method stub
		return personDAO.findPerson(p);
	}
}